<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="ThemeBucket">
    <link rel="shortcut icon" th:href="@{/favicon.ico}"/>
    <link rel="bookmark" th:href="@{/favicon.ico}"/>
    <title>LangV</title>

    <link th:href="@{/css/style.css}" rel="stylesheet">
    <link th:href="@{/css/style-responsive.css}" rel="stylesheet">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script th:src="@{/js/html5shiv.js}"></script>
    <script th:src="@{/js/respond.min.js}"></script>
    <![endif]-->
    <style> /*v-clock 解决闪烁{{后端参数}}的问题，在id=app的div内声明v-clock即可*/
    [v-clock]{
        display: none;
    }
    </style>
</head>

<body class="mybody">

<!-- left side start-->
<!-- left side end-->

<!-- main content start-->

<!-- header section start-->
<div th:replace="common/header::top"></div>
<!-- header section end-->

<!-- page heading start-->
<!-- page heading end-->

<!--body wrapper start-->
<div class="wrapper  " >
    <div class="row ">
        <div th:replace="common/header::#user_header(activeUrl='focus.html')"></div>

        <div id="app" v-clock class="col-sm-10" style="left: 8.3%">
            <div style="margin-top: -80px">
            <section class="panel">
                <div class="panel-body">
                    <div class="col-md-12 col-sm-6"  >
                        <div v-for="youtuber in youtubers"  >
                            <div class="panel-body">
                                <div class="media">
                                    <a class="pull-left" :href="'/user/toHome/'+(youtuber.username  )">
                                        <img style="border-radius: 50%;width: 75px; height: 75px" class="thumb media-object" src="/images/photos/user2.png" alt="">
                                    </a>
                                    <div class="media-body">
                                        <address>
                                            <br>
                                            <a  style="text-decoration: none"    :href="'/user/toHome/'+(youtuber.username  )">
                                            <strong style="font-size: large">{{youtuber.username}}</strong><br>
                                            </a>
                                            <div>{{youtuber.signature}}
                                                <span v-if="username===currentUsername"  >
                                                    <a :name="'sub'+(youtuber.username)" @click="subscribe($event)"    class="btn btn-danger subscribe_btn"  >订阅2.5万</a>
                                                    <a :name="'un_sub'+(youtuber.username)" @click="un_subscribe($event)" class="btn btn-danger subscribe_unbtn" >已订阅 2.5万</a>
                                                </span>
                                            </div>
                                        </address>
                                    </div>
                                </div>
                            </div>
                            <hr>
                        </div>
                        <div v-if="youtubers.length==0" >
                            <div class="panel-body">
                                <div class="media">
                                    <div class="media-body">
                                        <address>
                                            <br>
                                            <strong style="font-size: x-large;margin-left: 9cm">您还没有订阅任何人，快去订阅自己喜欢的作者吧！</strong><br>
                                            <div>
                                            </div>
                                        </address>
                                    </div>
                                </div>
                            </div>

                        </div>
                        </div>
                    </div>
            </section>
            </div>
        </div>

    </div>
</div>
<!--body wrapper end-->
<!--footer section start-->
<!--footer section end-->
<div th:replace="common/header::footer"></div>


<!-- m  ain content end-->

<!-- Placed js at the end of the document so the pages load faster -->
<script th:src="@{/js/jquery-1.10.2.min.js}"></script>
<script th:src="@{/js/jquery-ui-1.9.2.custom.min.js}"></script>
<script th:src="@{/js/jquery-migrate-1.2.1.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
<script th:src="@{/js/modernizr.min.js}"></script>
<!--右边滑条样式-->
<!--<script src="js/jquery.nicescroll.js"></script>-->

<script th:src="@{/js/jquery.isotope.js}"></script>

<!--common scripts for all pages-->
<script th:src="@{/js/scripts.js}"></script>
<script th:src="@{/js/vue.min.js}"></script>
<script th:src="@{/js/axios.min.js}"></script>
<script >
    new Vue({
        el: '#app',
        data: {
            youtubers: [],
            /*想完成定时任务先创建一个计算器timer*/
            timer: 0,
        },
        mounted(){
            //通过当前用户名获取到subscribe表信息
            axios.get('/subscription/search_focus/'+username).then(res=>{
                //遍历表信息
                res.data.forEach((subscribe)=>{
                    //把表中的被订阅者即youtuber字段作为username的查询条件查找其完整的user信息
                    axios.get('/user/getUsersInfo/'+subscribe.youtuber).then(res=>{
                        //得到user的信息再装入vue的youtubers数组中展现到前端页面
                        this.youtubers.push(res.data);

                    })
                })
            })
        },
        methods:{
            subscribe(event) {
                $("[name='"+event.target.name+"']").css('display', 'none');
                $("[name='"+"un_"+event.target.name+"']").css('display', 'inline');
                //切割subxxx为成为xxx然后传入订阅接口
                var youtuberName=(event.target.name).slice(3)
                /*两个定时方法的解释
                   1、 clearTimeout(this.timer)：当上面事件下来时，把唯一的timer清除，即置零
                   2、 this.timer = setTimeout(() =>{···},1000):
                       此时timer开始计时，计到1s时就开始执行大括号里面的函数;
                       如多次点击，那么就会不断触发上面的clearTimeout函数，
                       又会把timer给置零了,又重新计时。所以只有最后一次点击有效
                 */
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    axios.get('/subscription/subscribe/' + youtuberName).then(res => {
                        console.log("订阅成功");
                    })
                },1000)
            },
            un_subscribe(event){
                $("[name='"+event.target.name+"']").css('display', 'none');
                //将un_subxxx切割成subxxx使订阅按钮显示
                var subName=(event.target.name).slice(3);
                $("[name='"+subName+"']").css('display', 'inline');
                //再把subxxx切割成xxx传入取消订阅接口
                var youtuberName=subName.slice(3);
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    axios.get('/subscription/undo_subscribe/' + youtuberName).then(res => {
                        console.log("取消订阅成功");
                    })
                }, 1000)


            }

        }
    })
</script>


</body>
</html>
